package com.qiuzhiyi.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.li.utils.VerifyUtil;
import com.li.widget.DoubleTextView;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.base.QZYBaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description: 注册界面
 */

public class RegisterActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_msg)
    EditText mEtMsg;
    @BindView(R.id.txt_get_msg)
    TextView mTxtGetMsg;
    @BindView(R.id.txt_register)
    TextView mTxtRegister;
    @BindView(R.id.txt_protocol)
    DoubleTextView mTxtProtocol;
    private Count mCount;
    private EventHandler mEventHandler;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mCount = new Count(60 * 1000 + 16, 1000);
        //短信回调
        mEventHandler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoadingDialog();
                    }
                });
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //提交验证码成功
                                Bundle bundle=new Bundle();
                                bundle.putString("account",mEtPhone.getText().toString());
                                bundle.putString("pwd",mEtPwd.getText().toString());
                                startThenFinishWithArg(PerfectRegisterInfoActivity.class,bundle);
                            }
                        });


                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        mCount.start();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("获取验证码成功");
                            }
                        });
                    }
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("请正确填写手机号和验证码");
                        }
                    });
                }
            }
        };
        SMSSDK.registerEventHandler(mEventHandler);
    }

    @Override
    public void onFunctionTxtListener(View view) {
        super.onFunctionTxtListener(view);
        startThenFinish(LoginActivity.class);
    }

    @OnClick({R.id.txt_get_msg, R.id.txt_register, R.id.txt_protocol})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_get_msg:
                if (VerifyUtil.checkPhone(mEtPhone.getText().toString())) {
                    SMSSDK.getVerificationCode("+86",mEtPhone.getText().toString());
                    showLoadingDialog("正在获取验证码");
                }else {
                    showToast("请填写正确手机号");
                }
                break;
            case R.id.txt_register:
                if (isEmpty(mEtMsg)||isEmpty(mEtPhone)||mEtPwd.getText().toString().length()<6) {
                    showToast("请正确填写信息，且密码不少于6位");
                    return;
                }
                SMSSDK.submitVerificationCode("+86",mEtPhone.getText().toString(),mEtMsg.getText().toString());
                showLoadingDialog();


                break;
            case R.id.txt_protocol:
                break;
        }
    }
    private class Count extends CountDownTimer {

        Count(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTxtGetMsg.setEnabled(false);
            String txt = String.valueOf(millisUntilFinished / 1000) + "秒后重新获取";
            mTxtGetMsg.setText(txt);
        }

        @Override
        public void onFinish() {
            mTxtGetMsg.setEnabled(true);
            mTxtGetMsg.setText("重新获取验证码");
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mCount.cancel();
        SMSSDK.unregisterEventHandler(mEventHandler);
    }
}
